package com.example.mydemo;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.tal.pad.eye_protection.AwConDetectApi;
import com.tal.pad.eye_protection.ResultInfo;
import com.tal.pad.eye_protection.interfaces.NoPersonStatusCallback;
import com.tal.pad.eye_protection.interfaces.ProtectEyeResultCallback;
import com.tal.pad.eye_protection.util.AssetsUtil;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class CameraTestChildActivity extends AppCompatActivity {
    private final int MSG_STAR = 101;
    private final int MSG_STOP = 102;
    private final int MSG_SET_RESULT = 103;
    private final long TIME_WORK = 350L;
    private final long TIME_SLLEP = 2000L;
    private int starNum = 0;
    @Nullable
    private String[] fileNames;
    @NotNull
    private String assetsDirName = "pipeline_testimgs";
    private ImageView iv_img;
    private TextView tv_result;
    private Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_STAR:
                    if (starNum < fileNames.length) {
                        String fileName = fileNames[starNum];
                        Bitmap bitmap = AssetsUtil.getBitmapFromAsset(CameraTestChildActivity.this, fileName, assetsDirName);
                        if (bitmap==null){
                            return;
                        }
                        iv_img.setImageBitmap(bitmap);
                        byte[] data = AssetsUtil.convertBitmapToNV21(bitmap);
                        AwConDetectApi.getInstance().detectFrame(data, bitmap.getWidth(), bitmap.getHeight(), 0, 0, 1, "ID");
                        starNum++;
                        sendEmptyMessageDelayed(MSG_STOP, TIME_WORK);
                    }
                    break;
                case MSG_STOP:
                    AwConDetectApi.getInstance().getResult(new ProtectEyeResultCallback() {
                        @Override
                        public void onResult(ResultInfo resultInfo) {
                            Message msg0 =new  Message();
                            msg0.what=MSG_SET_RESULT;
                            msg0.obj=resultInfo;
                            sendMessage(msg0);
                        }
                    });

                sendEmptyMessageDelayed(MSG_STAR, TIME_SLLEP);
                    break;
                case MSG_SET_RESULT:
                    ResultInfo reBean = (ResultInfo) msg.obj;
                    String str = "趴着次数:" + reBean.prostrate_time + " 打哈欠次数:" + reBean.yawn_count +
                            " 瞌睡次数:" + reBean.sleep_time + " 低头次数:" + reBean.headdown_time +
                            " 俯视次数:" + reBean.lookdown_time + " 仰头次数:" + reBean.headup_time +
                            " 抬头次数:" + reBean.lookup_time + " 歪头次数:" + reBean.headtilt +
                            " 侧脸次数:" + reBean.faceprofile + " 东张西望次数:" + reBean.lookaround +
                            " 斜肩次数:" + reBean.shouldertilt;
                    tv_result.setText("结果：" + str);
//                    tv_result.setText("结果：" + new Gson().toJson(msg.obj));
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera_test_child);
        iv_img=findViewById(R.id.iv_img);
        tv_result=findViewById(R.id.tv_result);
        fileNames =AssetsUtil.getFilesFromAssets(this,assetsDirName);
        Log.e("+++", "onCreate: "+new Gson().toJson(fileNames));
        handler.sendEmptyMessage(MSG_STAR);
    }
    public void getResult(View view) {

        AwConDetectApi.getInstance().getResult(new ProtectEyeResultCallback() {
            @Override
            public void onResult(ResultInfo resultInfo) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        tv_result.setText("结果：" + new Gson().toJson(resultInfo));
                    }
                }) ;
            }
        });
    }
    public void resetResult(View view){
        AwConDetectApi.getInstance().resetResult();
    }

    public void getNoPerson(View view){
        AwConDetectApi.getInstance().getNoPersonStatus(new NoPersonStatusCallback() {
            @Override
            public void onNoPersonStatus(int status) {
                Log.e("+++", "onNoPersonStatus: "+status );
            }
        });
    }
}